package com.swk.common.util.config;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;
import org.springframework.web.context.ContextLoader;

import java.util.List;

/**
 * 配置文件加载 读取类
 *
 * @author wzy-pc
 */
public class ConfigLoad {
    private static PropertiesConfiguration config = null;

    static Logger logger = Logger.getLogger(ConfigLoad.class);

    static {
        try {
            String confPath = System.getenv("WZH_CONF_HOME");
            String contextPath = ContextLoader.getCurrentWebApplicationContext().getApplicationName();
            logger.error("Config file load path：" + confPath + contextPath + "/Config.properties");
            config = new PropertiesConfiguration(confPath + contextPath + "/Config.properties");
        } catch (Exception e) {
            logger.error("-----环境变量中的配置文件加载异常了，如果是生产环境看到此异常请配置环境变量--WZH_CONF_HOME=\"/xxxx/..../conf\"");
            try {
                config = new PropertiesConfiguration("Config.properties");
                logger.error("-----环境变量中的配置文件加载异常了，尝试成功加载了war中的配置文件，如果是生产部署请谨慎！！！");
            } catch (ConfigurationException e1) {
                logger.error("-----尝试加载war中的配置文件也失败了。。。", e1);
            }
        }
    }

    public static String getString(String key) {
        return config.getString(key);
    }

    public static String getString(String key, String def) {
        return config.getString(key, def);
    }

    public static int getInt(String key) {
        return config.getInt(key);
    }

    public static int getInt(String key, int def) {
        return config.getInt(key, def);
    }

    public static long getLong(String key) {
        return config.getLong(key);
    }

    public static long getLong(String key, long def) {
        return config.getLong(key, def);
    }

    public static float getFloat(String key) {
        return config.getFloat(key);
    }

    public static float getFloat(String key, float def) {
        return config.getFloat(key, def);
    }

    public static double getDouble(String key) {
        return config.getDouble(key);
    }

    public static double getDouble(String key, double def) {
        return config.getDouble(key, def);
    }

    public static boolean getBoolean(String key) {
        return config.getBoolean(key);
    }

    public static boolean getBoolean(String key, boolean def) {
        return config.getBoolean(key, def);
    }

    public static String[] getStringArray(String key) {
        return config.getStringArray(key);
    }

    public static List<?> getList(String key) {
        return config.getList(key);
    }

    public static List<?> getList(String key, List<?> def) {
        return config.getList(key, def);
    }

    public static void setProperty(String key, Object value) {
        config.setProperty(key, value);
    }

    public static void main(String[] args) {
        System.out.println(ConfigLoad.getString("mail22"));
        System.out.println(ConfigLoad.getString("mail.title"));
    }
}

